import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:wifi_iot/wifi_iot.dart';

void main() {
  // 测试枚举序列化
  group('Enum Serialization', () {
    test('serializeNetworkSecurityMap should contain all enum values', () {
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.WPA), true);
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.WEP), true);
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.NONE), true);
      
      expect(serializeNetworkSecurityMap[NetworkSecurity.WPA], 'WPA');
      expect(serializeNetworkSecurityMap[NetworkSecurity.WEP], 'WEP');
      expect(serializeNetworkSecurityMap[NetworkSecurity.NONE], 'NONE');
    });
  });

  // 测试APClient类的JSON解析
  group('APClient JSON Parsing', () {
    test('parse should correctly parse JSON string', () {
      const jsonString = '[{"IPAddr":"192.168.1.100","HWAddr":"00:11:22:33:44:55","Device":"Device1","isReachable":true}]';
      
      final clients = APClient.parse(jsonString);
      
      expect(clients.length, 1);
      expect(clients[0].ipAddr, '192.168.1.100');
      expect(clients[0].hwAddr, '00:11:22:33:44:55');
      expect(clients[0].device, 'Device1');
      expect(clients[0].isReachable, true);
    });

    test('toJson should correctly convert object to JSON', () {
      final client = APClient.fromJson({
        'IPAddr': '192.168.1.100',
        'HWAddr': '00:11:22:33:44:55',
        'Device': 'Device1',
        'isReachable': true
      });
      
      final json = client.toJson();
      
      expect(json['IPAddr'], '192.168.1.100');
      expect(json['HWAddr'], '00:11:22:33:44:55');
      expect(json['Device'], 'Device1');
      expect(json['isReachable'], true);
    });
  });

  // 测试WifiNetwork类的JSON解析（虽然已标记为废弃）
  group('WifiNetwork JSON Parsing (Deprecated)', () {
    test('parse should correctly parse JSON string', () {
      const jsonString = '[{"SSID":"TestSSID","BSSID":"00:11:22:33:44:55","capabilities":"[WPA-PSK]","frequency":2437,"level":-50,"timestamp":123456789}]';
      
      final networks = WifiNetwork.parse(jsonString);
      
      expect(networks.length, 1);
      expect(networks[0].ssid, 'TestSSID');
      expect(networks[0].bssid, '00:11:22:33:44:55');
      expect(networks[0].capabilities, '[WPA-PSK]');
      expect(networks[0].frequency, 2437);
      expect(networks[0].level, -50);
      expect(networks[0].timestamp, 123456789);
    });

    test('toJson should correctly convert object to JSON', () {
      final network = WifiNetwork.fromJson({
        'SSID': 'TestSSID',
        'BSSID': '00:11:22:33:44:55',
        'capabilities': '[WPA-PSK]',
        'frequency': 2437,
        'level': -50,
        'timestamp': 123456789
      });
      
      final json = network.toJson();
      
      expect(json['SSID'], 'TestSSID');
      expect(json['BSSID'], '00:11:22:33:44:55');
      expect(json['capabilities'], '[WPA-PSK]');
      expect(json['frequency'], 2437);
      expect(json['level'], -50);
      expect(json['timestamp'], 123456789);
    });
  });

  // 测试枚举序列化
  group('Enum Serialization', () {
    test('serializeNetworkSecurityMap should contain all enum values', () {
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.WPA), true);
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.WEP), true);
      expect(serializeNetworkSecurityMap.containsKey(NetworkSecurity.NONE), true);
      
      expect(serializeNetworkSecurityMap[NetworkSecurity.WPA], 'WPA');
      expect(serializeNetworkSecurityMap[NetworkSecurity.WEP], 'WEP');
      expect(serializeNetworkSecurityMap[NetworkSecurity.NONE], 'NONE');
    });
  });

  // 测试APClient类的JSON解析
  group('APClient JSON Parsing', () {
    test('parse should correctly parse JSON string', () {
      const jsonString = '[{"IPAddr":"192.168.1.100","HWAddr":"00:11:22:33:44:55","Device":"Device1","isReachable":true}]';
      
      final clients = APClient.parse(jsonString);
      
      expect(clients.length, 1);
      expect(clients[0].ipAddr, '192.168.1.100');
      expect(clients[0].hwAddr, '00:11:22:33:44:55');
      expect(clients[0].device, 'Device1');
      expect(clients[0].isReachable, true);
    });

    test('toJson should correctly convert object to JSON', () {
      final client = APClient.fromJson({
        'IPAddr': '192.168.1.100',
        'HWAddr': '00:11:22:33:44:55',
        'Device': 'Device1',
        'isReachable': true
      });
      
      final json = client.toJson();
      
      expect(json['IPAddr'], '192.168.1.100');
      expect(json['HWAddr'], '00:11:22:33:44:55');
      expect(json['Device'], 'Device1');
      expect(json['isReachable'], true);
    });
  });

  // 测试WifiNetwork类的JSON解析（虽然已标记为废弃）
  group('WifiNetwork JSON Parsing (Deprecated)', () {
    test('parse should correctly parse JSON string', () {
      const jsonString = '[{"SSID":"TestSSID","BSSID":"00:11:22:33:44:55","capabilities":"[WPA-PSK]","frequency":2437,"level":-50,"timestamp":123456789}]';
      
      final networks = WifiNetwork.parse(jsonString);
      
      expect(networks.length, 1);
      expect(networks[0].ssid, 'TestSSID');
      expect(networks[0].bssid, '00:11:22:33:44:55');
      expect(networks[0].capabilities, '[WPA-PSK]');
      expect(networks[0].frequency, 2437);
      expect(networks[0].level, -50);
      expect(networks[0].timestamp, 123456789);
    });

    test('toJson should correctly convert object to JSON', () {
      final network = WifiNetwork.fromJson({
        'SSID': 'TestSSID',
        'BSSID': '00:11:22:33:44:55',
        'capabilities': '[WPA-PSK]',
        'frequency': 2437,
        'level': -50,
        'timestamp': 123456789
      });
      
      final json = network.toJson();
      
      expect(json['SSID'], 'TestSSID');
      expect(json['BSSID'], '00:11:22:33:44:55');
      expect(json['capabilities'], '[WPA-PSK]');
      expect(json['frequency'], 2437);
      expect(json['level'], -50);
      expect(json['timestamp'], 123456789);
    });
  });
}